import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { OrganizationLevelRelation } from '../domain/level';
import { OrganizatioinRoleRelationPK, OrganizationRoleRelation } from '../domain/organizatioin-role';
import { Organization } from '../domain/organization';
import { Role, RoleRelation } from '../domain/sparrow';

@Injectable({
  providedIn: 'root'
})
export class RoleService {
  delete(id: string) {
    return this.httpClient.delete(this.host+"/roles/"+id);
  }
  getParentOrganizations(id: any) {
    return this.httpClient.get<Organization[]>(this.host+"/roles/getParentOrganizations",{ params:{roleId:id}})
  }


  host:string = "http://localhost:8091/api";

  constructor(private httpClient: HttpClient) { }

  public getChildren(organizationId:string,parentId: string){
    return this.httpClient.get<any>(this.host+"/roles/getChildren",{ params:{organizationId:organizationId,parentId:parentId}})
  }

  getParents(organizationId:string,roleId: string) {
    return this.httpClient.get<any>(this.host+"/roles/getParents",{ params:{organizationId:organizationId,roleId:roleId}})
  }

  public getTreeByParentId(parentId: string){
    return this.httpClient.get<any>(this.host+"/roles/getTreeByParentId",{ params:{parentId:parentId}})
  }

  public saveBatch(roles: Role[]){
    return this.httpClient.post(this.host+"/roles/batch",roles);
  }

  public save(role: Role){
    if(role.id===undefined){
      return this.httpClient.post<Role>(this.host+"/roles",role);
    }else{
      return this.httpClient.patch<Role>(this.host+"/roles/" + role.id,role);
    }
  }

  public saveRelations(roleRelations: OrganizatioinRoleRelationPK[]){
    return this.httpClient.post(this.host+"/roles/relations",roleRelations);
  }

  public getModelName(){
    return this.httpClient.get<any>(this.host+"/roles/getModelName");
  }

  deleteBatch(ids$: any) {
    return this.httpClient.delete(this.host+"/roles/batch",{body:ids$});
  }

}
